/*
 * @Description: 
 * @Author: houcw
 * @Date: 2024-01-25 14:53:09
 * @LastEditTime: 2024-01-25 14:57:42
 * @LastEditors: houcw
 * @Reference: 
 */
let express = require('express');
let router = express.Router();
let Part = require('../model/Part');
// let historyEntry = require('../controller/history')


/**
 * 查询所有成分列表
 */
router.get("/list", (req, res) => {
    Part.find({}, (err, data) => {
      if (err) {
        res.json({
          status: 404,
          msg: err.message
        });
      } else {
        res.json({
          status: 200,
          msg: '查询成功',
          list: data
        })
      }
    });
  });
  
  /**
   * 查询成分列表（分页）
   */
  router.get("/listByPage", (req, res) => {
    const size = Number(req.query.size)
    const page = Number(req.query.page)
    Part.find().limit(Number(size)).skip((page - 1) * size).exec((err,data) => {
      if (err) {
        res.json({
          status: 404,
          msg: err.message
        });
      } else {
        res.json({
          status: 200,
          msg: '查询成功',
          list: data,
          total:data.length
        })
      }
    });
  })
  
  /**
   * 根据成分id查询成分详情
   */
  router.get("/partInfo/:id", (req, res) => {
    const id = String(req.params.id)
    Part.findById(id, (err, data) => {
      if (err) {
        res.json({
          status: 404,
          msg: err.message
        });
      } else {
        res.json({
          status: 200,
          msg: '查询成功',
          list: data
        })
      }
    })
  })
  /**
   * 新增成分
   */
  router.post("/addPart",  (req, res) => {
    const name = req.body.name
    Part.findOne({name: name}, (err, data) => {
      if (err) {
        res.json({
          status: 400,
          msg: "数据读取失败！"
        });
      } else {
        Part.create({...req.body}, (err1, data1) => {
            if (err1) {
              res.json({
                status: 400,
                msg: err.message
              });
            } else {
              res.json({
                 status: 200,
                 msg: '新增成功'
              })
            }
          })
      }
    })
  })
  
  /**
   * 删除成分
   */
  router.delete("/deletePartById/:id", (req, res) => {
    const id = String(req.params.id)
    Part.findOne({_id: id}, (err, data) => {
      if (err) {
        res.json({
          status: 400,
          msg: "数据读取失败！"
        });
      } else {
        if (data) {
          res.json({
            status: 200,
            msg: '删除成功'
          })
        } else {
          res.json({
            status: 400,
            msg: "当前成分不存在！"
          });
        }
      }
    })
  })
  
  /**
   * 修改成分
   */
  router.put("/updatePart/:id", (req, res) => {
    const _id = String(req.params.id)
    const { price, MJProportion, proteinProportion, calciumProportion, phosphorusProportion } = req.body
    Part.findOne({_id}, (err, data) => {
      if (err) {
        res.json({
          status: 400,
          msg: "数据读取失败！"
        });
      } else {
        if (data) {
          Part.updateOne({_id}, {
            price,
            MJProportion,
            proteinProportion,
            calciumProportion,
            phosphorusProportion
          }, (err1, data1) => {
            if (err1) {
              res.json({
                status: 400,
                msg: err.message
              });
            } else {
              res.json({
                 status: 200,
                 msg: '修改成功！'
               })
            }
          })
        } else {
          res.json({
            status: 400,
            msg: "当前成分不存在！"
          });
        }
      }
    })
  })
  module.exports = router;